﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTour : AdminPageBase
    {
       private TourInfo _tourInfo = new TourInfo();
        private readonly TourController _tourController = new TourController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
                _tourInfo = _tourController.GetTour(int.Parse(Request.QueryString["TourId"]));

            if (Page.IsPostBack) return;

            LoadSchedule();
            LoadLocationBegin();
            LoadGroupType();
            LoadTourType();
            LoadStatus();
            LoadCreatedBy();
            LoadTime();
            LoadMartName();
            LoadSeason();

            if (_tourInfo.TourId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListTour.aspx?xml=Tour");
        }

        private void SaveData()
        {
            _tourInfo.TourName = txtTourName.Text;
            _tourInfo.TourTypeId = int.Parse(ddlTourType.SelectedValue);
            _tourInfo.GroupType = int.Parse(ddlGroupType.SelectedValue);
            _tourInfo.LocationBegin = int.Parse(ddlLocationBegin.SelectedValue);
            _tourInfo.ScheduleId = int.Parse(ddlSchedule.SelectedValue);
            _tourInfo.QuantityAdult = int.Parse(txtQuantityAdult.Text);
            _tourInfo.QuantityChild = int.Parse(txtQuantityChild.Text);
            _tourInfo.Season = ddlSeason.Text;
            _tourInfo.MartName = ddlMartName.SelectedValue;
            if (!String.IsNullOrEmpty(dteBeginDate.Value))
            {
                var beginDate = Convert.ToDateTime(dteBeginDate.Value);
                _tourInfo.BeginDate =  new DateTime(beginDate.Year, beginDate.Month,beginDate.Day,int.Parse(ddlBeginHour.SelectedValue),int.Parse(ddlBeginMinute.SelectedValue),0);
            }
            if (!String.IsNullOrEmpty(dteEndDate.Value))
            {
                var endDate = Convert.ToDateTime(dteEndDate.Value);
                _tourInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHour.SelectedValue), int.Parse(ddlEndMinute.SelectedValue), 0);
            }
            _tourInfo.QuantityDay= int.Parse(txtQuantityDay.Text);
            _tourInfo.QuantityNight = int.Parse(txtQuantityNight.Text);
            _tourInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_tourInfo.TourId == -1)
            {
                _tourInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _tourInfo.CreatedDate = DateTime.Now;
                _tourController.InsertTour(_tourInfo);
            }
            else
            {
                _tourInfo.UpdatedBy = UserId;
                _tourInfo.UpdatedDate = DateTime.Now;
                _tourController.UpdateTour(_tourInfo);
            }
            Response.Redirect("/Pages/ListTour.aspx?xml=Tour");
        }

        private void LoadTime()
        {
            for (var i = 0; i < 24; i++)
            {
                var hour = i < 10 ? "0" + i : i.ToString();
                ddlBeginHour.Items.Add(new ListItem(hour, hour));
                ddlEndHour.Items.Add(new ListItem(hour, hour));
            }
            for (var i = 0; i < 60; i++)
            {
                var minute = i < 10 ? "0" + i : i.ToString();
                ddlBeginMinute.Items.Add(new ListItem(minute, minute));
                ddlEndMinute.Items.Add(new ListItem(minute, minute));
            }
        }
        private void RenderForm()
        {
            txtTourName.Text = _tourInfo.TourName;
            dteBeginDate.Value = _tourInfo.BeginDate.ToString("dd/MM/yyyy");
            dteEndDate.Value = _tourInfo.EndDate.ToString("dd/MM/yyyy");
            ddlStatus.SelectedValue = _tourInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _tourInfo.CreatedBy.ToString();
            ddlBeginHour.SelectedValue = _tourInfo.BeginDate.Hour.ToString();
            ddlEndHour.SelectedValue = _tourInfo.EndDate.Hour.ToString();
            ddlBeginMinute.SelectedValue = _tourInfo.BeginDate.Minute.ToString();
            ddlEndMinute.SelectedValue = _tourInfo.EndDate.Minute.ToString();
            ddlTourType.SelectedValue = _tourInfo.TourTypeId.ToString();
            ddlGroupType.SelectedValue = _tourInfo.GroupType.ToString();
            ddlLocationBegin.SelectedValue = _tourInfo.LocationBegin.ToString();
            ddlSchedule.SelectedValue = _tourInfo.ScheduleId.ToString();
            ddlSeason.SelectedValue = _tourInfo.Season;
            ddlMartName.SelectedValue = _tourInfo.MartName;
            txtQuantityAdult.Text = _tourInfo.QuantityAdult.ToString();
            txtQuantityChild.Text = _tourInfo.QuantityChild.ToString();
            txtQuantityDay.Text = _tourInfo.QuantityDay.ToString();
            txtQuantityNight.Text = _tourInfo.QuantityNight.ToString();

        }

        private void LoadTourType()
        {
            ddlTourType.DataSource = new TourTypeController().ListTourType().Where(p=>p.Status==(int)Globals.DocumentStatus.Use);
            ddlTourType.DataTextField = "TourTypeName";
            ddlTourType.DataValueField = "TourTypeId";
            ddlTourType.DataBind();

            ddlTourType.Items.Insert(0, new ListItem("--- Chọn loại tour ---", "-1"));
        }
        private void LoadMartName()
        {
            ddlMartName.Items.Insert(0, new ListItem("--- Chọn thị trường ---", "-1"));
            ddlMartName.Items.Insert(1, new ListItem("Inbound", "Inbound"));
            ddlMartName.Items.Insert(2, new ListItem("Outbound", "Outbound"));
            ddlMartName.Items.Insert(3, new ListItem("Nội địa", "Nội địa"));
        }
        private void LoadSeason()
        {
            ddlSeason.Items.Add("Thấp điểm");
            ddlSeason.Items.Add("Cao điểm");
            ddlSeason.Items.Add("Cực cao điểm");

            ddlSeason.Items.Insert(0, new ListItem("---Chọn mùa du lịch---", "-1"));
        }
        private void LoadGroupType()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/GroupType.xml"));
            var dv = ds.Tables["GroupType"].DefaultView;
            ddlGroupType.DataTextField = "Name";
            ddlGroupType.DataValueField = "ID";
            ddlGroupType.DataSource = dv;
            ddlGroupType.DataBind();
            ddlGroupType.Items.Insert(0, new ListItem("--- Chọn loại hình đoàn ---", "-1"));
        }
        private void LoadLocationBegin()
        {
            ddlLocationBegin.DataSource = new LocationController().ListLocation().Where(p => p.Status == (int)Globals.DocumentStatus.Use);
            ddlLocationBegin.DataTextField = "LocationName";
            ddlLocationBegin.DataValueField = "LocationId";
            ddlLocationBegin.DataBind();

            ddlLocationBegin.Items.Insert(0, new ListItem("--- Chọn điềm khởi hành ---", "-1"));
        }

        private void LoadSchedule()
        {
            ddlSchedule.DataSource = new ScheduleController().ListSchedule().Where(p => p.Status == (int)Globals.DocumentStatus.Use);
            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
        private void LoadCreatedBy()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "Username";
            ddlCreatedBy.DataValueField = "UserID";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}